import routes from "@/config/routes"
import ErrorPage from '@/pages/ErrorPage'

/**
 * @description: 以递归的方式展平react router数组
 * @param {routerConfigModel[]} arr 路由数组
 */
export const flattenRoutes = (arr) =>
    arr.reduce((prev, item) => {
        if (Array.isArray(item.routes)) {
            prev.push(item)
        }
        return prev.concat(Array.isArray(item.routes) ? flattenRoutes(item.routes) : item)
    }, [])

/**
 * @description: 根据路径获取路由的name和key
 * @param {string} path 路由
 * @return {*}
 */
export const getKeyName = (path = '/403') => {
    const truePath = path.split('?')[0]
    const curRoute = flattenRoutes(routes).filter((item) =>
        item.path.includes(truePath)
    )
    if (!curRoute[0]) return { title: '暂无权限', tabKey: '403', component: ErrorPage }
    const { name, key, component } = curRoute[0]
    return { title: name, tabKey: key, component }
}